package com.example.controller;

import java.util.ArrayList;
import java.util.List;

import com.example.model.UserCRUD;

public class ServiceStatus {

	int serviceStatusID;
	int serviceID;
	int serviceOwnerID;
	int requesterUserID;
	boolean isConnected;
	boolean isPaid;
	String timeStamp;
	iCRUD<ServiceStatus, Integer, Integer> crudObject;
	
	public ServiceStatus(iCRUD<ServiceStatus, Integer, Integer> crudObject){
		this.crudObject = crudObject;
	}
	
	public void create()
	{
		this.crudObject.create(this);
	}
	
	public ServiceStatus read(Integer ssID){
		return this.crudObject.read(ssID);
	}
	
	public List<ServiceStatus> readAll(){
		return this.crudObject.readAll();
	}
	
	public List<ServiceStatus> readAllStatusesForOtherServices(int userID){
		List<ServiceStatus> all = this.crudObject.readAll();
		
		
		List<ServiceStatus> filtered = new ArrayList<ServiceStatus>();
		
		for(int i = 0; i<all.size(); i++){
			
			if(all.get(i).getRequesterUserID() == userID){
				
				filtered.add(all.get(i));
			}
		}
		
		return filtered;
		
	}
	
	@Override
	public String toString(){
		User expert = new User(new UserCRUD());
		String name = expert.read(serviceOwnerID).getFirstName();
		String text = "Service requested from "+name+" at "+timeStamp.toString();
		return text;
	}
	
	public List<ServiceStatus> readAllStatusesFromMyOfferedServices(int userID){
		List<ServiceStatus> all = this.crudObject.readAll();
		
		List<ServiceStatus> filtered = new ArrayList<ServiceStatus>();
		
		for(int i = 0; i<all.size(); i++){
			if(all.get(i).getServiceOwnerID() == userID){
				filtered.add(all.get(i));
			}
		}
		
		return filtered;
	}
	
	public List<ServiceStatus> readAllStatusesForService(int serviceID) {
		List<ServiceStatus> all = this.crudObject.readAll();
		
		List<ServiceStatus> filtered = new ArrayList<ServiceStatus>();
		
		for(int i = 0; i<all.size(); i++){
			if(all.get(i).getServiceID() == serviceID){
				filtered.add(all.get(i));
			}
		}
		
		return filtered;
	}
	
	public void update(){
		this.crudObject.update(this);
	}
	
	public void delete(){
		this.crudObject.delete(this.serviceStatusID);
	}
	
	
	//Getters and Setters
	public int getServiceStatusID() {
		return serviceStatusID;
	}
	public void setServiceStatusID(int serviceStatusID) {
		this.serviceStatusID = serviceStatusID;
	}
	public int getServiceID() {
		return serviceID;
	}
	public void setServiceID(int serviceID) {
		this.serviceID = serviceID;
	}
	public int getServiceOwnerID() {
		return serviceOwnerID;
	}
	public void setServiceOwnerID(int serviceOwnerID) {
		this.serviceOwnerID = serviceOwnerID;
	}	
	public int getRequesterUserID() {
		return requesterUserID;
	}
	public void setRequesterUserID(int requesterUserID) {
		this.requesterUserID = requesterUserID;
	}
	public boolean isConnected() {
		return isConnected;
	}
	public void setConnected(boolean isConnected) {
		this.isConnected = isConnected;
	}
	public boolean isPaid() {
		return isPaid;
	}
	public void setPaid(boolean isPaid) {
		this.isPaid = isPaid;
	}
	public String getTimeStamp() {
		return timeStamp;
	}
	public void setTimeStamp(String timeStamp) {
		this.timeStamp = timeStamp;
	}
	
	
	
}
